package client

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"opd_server/proto/opd"
)

type OpdHandler func(ctx context.Context, client opd.OpdClient) (interface{}, error)

func OpdClient(ctx context.Context, handler OpdHandler) (interface{}, error) {
	dial, err := grpc.Dial(fmt.Sprintf("%s:%d", "127.0.0.1", 9998), grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := opd.NewOpdClient(dial)
	return handler(ctx, client)
}
func OutpatientRegistration(ctx context.Context, in *opd.OutpatientRegistrationRequest) (*opd.OutpatientRegistrationResponse, error) {
	client, err := OpdClient(ctx, func(ctx context.Context, client opd.OpdClient) (interface{}, error) {
		res, err := client.OutpatientRegistration(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*opd.OutpatientRegistrationResponse), nil
}

func DoctorList(ctx context.Context, in *opd.DoctorListRequest) (*opd.DoctorListResponse, error) {
	client, err := OpdClient(ctx, func(ctx context.Context, client opd.OpdClient) (interface{}, error) {
		res, err := client.DoctorList(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*opd.DoctorListResponse), nil
}

func DoctorInfo(ctx context.Context, in *opd.DoctorInfoRequest) (*opd.DoctorInfoResponse, error) {
	client, err := OpdClient(ctx, func(ctx context.Context, client opd.OpdClient) (interface{}, error) {
		res, err := client.DoctorInfo(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*opd.DoctorInfoResponse), nil
}

func AiConsultation(ctx context.Context, in *opd.AiConsultationRequest) (*opd.AiConsultationResponse, error) {
	client, err := OpdClient(ctx, func(ctx context.Context, client opd.OpdClient) (interface{}, error) {
		res, err := client.AiConsultation(ctx, in)
		if err != nil {
			return nil, err
		}
		return res, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*opd.AiConsultationResponse), nil
}
